package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.domain.OrderDetail;
import com.qf.domain.User;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public User selectByUsername(String username) {
        String sql=" select * from tb_user where username=? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(User.class),username);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insert(User user) {
        String sql=" insert into tb_user values (null,?,?,?,?,?,?,?) ";
        Object [] params={user.getUsername(),user.getPassword(),user.getEmail(),user.getGender(),user.getFlag(),user.getRole(),user.getCode()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public User login(String username, String password) {
        String sql=" SELECT * FROM `tb_user` WHERE `username`= ? AND `password`= ? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(User.class), username,password);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<User> findByRole() {
        String sql=" SELECT `id`,`username`,`password`,`email`,`gender`,`flag`,`role`,`code` FROM `tb_user` WHERE `role`=0 ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(User.class));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<User> findByNameAndGender(String where, List<Object> params) {
        String sql=" SELECT `id`,`username`,`password`,`email`,`gender`,`flag`,`role`,`code` FROM `tb_user` "+where+" AND `flag`=1 ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(User.class),params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void removeByUId(int uid) {
        //String sql1=" DELETE FROM `tb_orderdetail` WHERE `oid`= ? ";
        String sql2=" DELETE  FROM `tb_order` WHERE `uid`=? ";
        String sql3=" DELETE FROM `tb_address` WHERE `uid` =? ";
        String sql4=" DELETE FROM `tb_cart`  WHERE `id`=? ";
        String sql5=" DELETE FROM `tb_user` WHERE `id`=? ";


        try {
            //queryRunner.update(sql1,oid);
            queryRunner.update(sql2,uid);
            queryRunner.update(sql3,uid);
            queryRunner.update(sql4,uid);
            queryRunner.update(sql5,uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<User> findNoByNameAndGender(String where, List<Object> params) {
        String sql=" SELECT `id`,`username`,`password`,`email`,`gender`,`flag`,`role`,`code` FROM `tb_user` "+where+" AND `flag`=0 ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(User.class),params.toArray());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void modifyFlag1ById(int uid) {
        String sql=" UPDATE `tb_user` SET `flag`=1 WHERE `id`=? ";
        try {
            queryRunner.update(sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void modifyFlag0ById(int uid) {
        String sql=" UPDATE `tb_user` SET `flag`=0 WHERE `id`=? ";
        try {
            queryRunner.update(sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteBydetail(String id) {
        String sql=" DELETE FROM `tb_orderdetail` WHERE `oid`= ? ";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void modifyUserById(String id, String username, String email, String gender) {
        String sql=" UPDATE `tb_user` SET `username`=? ,`email`=? ,`gender`=? WHERE `id`=? ";
        try {
            queryRunner.update(sql,username,email,gender,id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public User findByUId(Integer uid) {
        String sql = " SELECT * FROM tb_user WHERE id=? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(User.class),uid);
        } catch (SQLException e) {
            e.getMessage();
            throw new RuntimeException(e);
        }
    }
}
